#!/bin/bash
# ********************************************************************
# 试验环境：CentOS 7.x
# Date: 2025-05-06
# Description：test script
# author Gavin
# email: lushuan2071@126.com
# command: sh init.sh|tee  init.log
# ********************************************************************

# CentOS 7- 配置阿里镜像源
function init_config_aliyun_repository() {
    ping mirrors.aliyun.com
    # 1. 下载CentOS 7的repo文件
    mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
    curl -o /etc/yum.repos.d/CentOS-Base-ali.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    # 下载 CentOS 8的repo文件
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
    # 2. 更新yum 源
    yum clean all
    yum makecache
    # 3. 备份配置文件
    sed -i 's/http:/https:/g' /etc/yum.repos.d/CentOS-Base-ali.repo
    # 4. 更新生效
    # 清除yum缓存
    yum clean all
    # 缓存阿里云源，比较耗费时间，空闲时间时执行
    yum makecache
    # 测试阿里云源
    yum list|wc -l


}

# 0.上传文件工具 基础安装工具
function base_tools() {
   yum -y install lrzsz git wget -y
   # 快速查找文件的命令
   yum -y install mlocate -y
   yum install -y vim lrzsz gcc gcc-c++ openssl-devel gddevel pam-devel pam expat-devel pcre-devel GeoIP-devel bison \
   ncurses ncurses-devel nfs-utils make unzip zip openssl openssl-devel cmake libaio openssh-clients perlTime-HiRes mtr openssh-clients net-tools iptables-services tree initscripts ntpdate wget nc
   # others
   yum install tree -y
   yum install python3 -y
}


# 安装helm 参考 https://blog.csdn.net/BigData_Mining/article/details/88794430
function install_helm() {
    wget https://get.helm.sh/helm-v3.7.0-linux-amd64.tar.gz
    tar -zxvf helm-v3.7.0-linux-amd64.tar.gz
    mv linux-amd64/helm /usr/local/bin/helm
}

# 0. 提示符（prompt）来显示当前的时间
function config_hostname() {
vi /etc/bashrc
# 最后一行添加
export PS1="[\u@\h \W]<\D{%Y%m%d %H:%M:%S}>\$"
..
source /etc/bashrc
}

# 1. 配置主机名
function config_hostname() {
hostnamectl set-hostname k8s-master1 && bash
}

# 2. 关闭 selinux
function close_selinux() {
# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

# 关闭 iptables 防火墙
yum install iptables-services -y
禁用 iptables
service iptables stop && systemctl disable iptables

# 清空防火墙规则
iptables -F

setenforce 0

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g'  /etc/selinux/config
# 注意：修改 selinux 配置文件之后，重启机器，selinux 才能永久生效

# 显示 Disabled 表示 selinux 关闭成功,getenforce 查看 SElinux 是否关闭
getenforce
}



# 3. 配置时间同步,必须执行 https://www.cnblogs.com/lizhaoxian/p/11260041.html
function ntp() {
yum install -y ntp ntpdate chrony
ntpdate cn.pool.ntp.org
systemctl daemon-reload
systemctl start chronyd
systemctl enable chronyd
# 查看同步状态
systemctl status chronyd -l

# 编写计划任务
crontab -e
systemctl restart crond
}


# 4. 安装基础软件包
function install_base_packages() {
yum install -y wget net-tools nfs-utils lrzsz gcc gcc-c++  \
make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim \
ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server \
socat ipvsadm conntrack
}

# 5. 安装docker-ce
function docker_install() {
#  安装docker依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2 -y
# 配置 docker-ce 国内 yum 源（阿里云）
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 安装docker-ce
yum install docker-ce -y
# 启动docker 服务
systemctl start docker && systemctl enable docker && systemctl status docker
# 查看docker 版本信息
docker version

# docker 阿里云镜像加速器地址 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
# 不止阿里云镜像加速器额外新增其它加速器地址
sudo mkdir -p /etc/docker
# docker 镜像源
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://jockerhub.com",
    "https://docker.m.daocloud.io",
    "https://dockerproxy.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://docker.nju.edu.cn",
    "https://registry.docker-cn.com",
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"],
  "live-restore": true
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker
sudo systemctl reload docker # 启用 live-restore 特性时，重启docker 容器不会退出

}

# 6. 修改终端颜色
function update_terminal_color() {

# 针对 Bash
echo 'export PS1="[\u@\h \W]<\D{%Y%m%d %H:%M:%S}>\$ "' >> ~/.bashrc
source ~/.bashrc  # 立即生效

# 针对 Zsh
echo 'export PS1="[\u@\h \W]<\D{%Y%m%d %H:%M:%S}>\$ "' >> ~/.zshrc
source ~/.zshrc


}

# 7. 关闭防火墙
function close_firewalld() {
systemctl disable --now firewalld && systemctl is-enabled firewalld
systemctl status firewalld
}

# 开启时间同步
function timedatesync() {
# 查看ntp 时间同步是否开启
timedatectl status
# 没有开启则进行开启
timedatectl set-ntp true

# 通过安装chrony 设置集群时间同步
yum install chrony -y

systemctl enable --now chronyd.service
systemctl restart chronyd.service
systemctl status chronyd.service

# 修改时钟同步配置文件
vi /etc/chrony.conf
# 阿里云
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst
server ntp4.aliyun.com iburst
# centos 默认
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

}

# 修改会话保持
# ClientAliveInterval 0和ClientAliveCountMax 3的注释符号去掉,将ClientAliveInterval对应的0改成60,ClientAliveInterval指定了服务器端向客户端请求消息的时间间隔,
# 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax,
# 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应
function update_ssh_alive() {
vim /etc/ssh/sshd_config
..
ClientAliveInterval 120 # 这里设置为两分钟
ClientAliveCountMax 3
...

systemctl restart sshd
systemctl status sshd

}

# 进程
function init_config_aliyun_repository() {
  pgrep java
  pidof java

}

function others() {
    # 查看服务的环境变量
    sudo systemctl show --property=Environment k3s.service
}